package com.rfid.emergencywarehouse.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.navigation.fragment.NavHostFragment;

import com.rfid.base.util.AnimatorHelper;
import com.rfid.emergencywarehouse.R;
import com.rfid.emergencywarehouse.adapter.AddAssetScanBarcodeAdapter;
import com.rfid.emergencywarehouse.adapter.MutilChoiceScanBarCodeAdapter;
import com.rfid.emergencywarehouse.adapter.ScanBarcodeAdapter;
import com.rfid.emergencywarehouse.entity.BarcodeInfo;
import com.rfid.emergencywarehouse.view.IAddAssetView;
import com.rfid.emergencywarehouse.entity.NameValueBean;
import com.rfid.emergencywarehouse.manager.FragmentDataManager;
import com.rfid.emergencywarehouse.presenter.AddAssetPresenter;
import com.rfid.emergencywarehouse.util.DialogUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Optional;

public class AddAssetMainFragment extends BaseScanListFragment<MutilChoiceScanBarCodeAdapter, BarcodeInfo,AddAssetPresenter> implements IAddAssetView, FragmentDataManager.Listener, CompoundButton.OnCheckedChangeListener {

    @BindView(R.id.title)
    TextView mTitle;

    NameValueBean mSort,mMaterial,mMaterailMode,mMaterailStatus;
    List<NameValueBean> mSorts,mMaterails,mMaterialModes,mMaterialStatusList;

    int mClickId = 0;
    @BindView(R.id.sort)
    View mSortView;
    @BindView(R.id.material)
    View mMaterialView;
    @BindView(R.id.materialMode)
    View mMaterialModeView;

    @BindView(R.id.sort_name)
    TextView mSortName;
    @BindView(R.id.material_name)
    TextView mMaterialName;
    @BindView(R.id.material_mode_name)
    TextView mMaterialModeName;
    @BindView(R.id.material_status_name)
    TextView mMaterialStatusName;
    @BindView(R.id.barcode)
    EditText mBarcodeEdit;

    @BindView(R.id.root)
    View mRoot;

    @BindView(R.id.scan)
    CheckBox mRfid;

    @Override
    public AddAssetPresenter getPresenter() {
        return new AddAssetPresenter();
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        FragmentDataManager.INSTANCE.addListener(this);
        mPresenter.getMaterialCategoryList();
        mPresenter.getWGStatusConfigs();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        FragmentDataManager.INSTANCE.removeListener(this);
    }


    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_addasset_main;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this,view);
        setTitle("新增物资");
//        for (int i=0;i<6;i++) {
//            BarcodeInfo info = new BarcodeInfo();
//            info.setBarcode(i + "");
//            mData.add(info);
//        }
//        mAdapter.notifyDataSetChanged();
//        mMaterialModeView.setEnabled(false);
//        mMaterialView.setEnabled(false);
    }

    @Override
    protected int getScanRfidCheckBoxId() {
        return R.id.scan;
    }

    @Override
    protected EditText getBarcodeEdit() {
        return mBarcodeEdit;
    }

    @Override
    protected MutilChoiceScanBarCodeAdapter getAdapter() {
        return new AddAssetScanBarcodeAdapter(mData);
    }

    @Override
    public void onGetMateralModeList(List<NameValueBean> beans) {
        System.out.println("onGetMateralModeList");
        mMaterialModes = beans;
        FragmentDataManager.INSTANCE.setData(beans);
    }

    @Override
    public void onGetMaterialList(List<NameValueBean> beans) {
        System.out.println("onGetMaterialList");
        mMaterails = beans;
        FragmentDataManager.INSTANCE.setData(beans);
    }

    @Override
    public void onGetMaterialCategoryList(List<NameValueBean> beans) {
        System.out.println("onGetMaterialCategoryList");
        mSorts = beans;
        FragmentDataManager.INSTANCE.setData(beans);
    }

    @Override
    public void onGetWGStatusConfigs(List<NameValueBean> beans) {
        mMaterialStatusList = beans;
        FragmentDataManager.INSTANCE.setData(beans);
    }


    @Override
    protected String getScanTip(int size) {
        if (mSelects.size() > 0) {
            return String.format("已扫描数据%s条，已选中%s条",size,mSelects.size());
        }
        return String.format("已扫描数据%s条，请选择你要新增的物资",size);
    }


    @Override
    public void onSelect(NameValueBean v) {
        System.out.println("onSelect = " + v);
        FragmentDataManager.INSTANCE.select(null);
        if (mClickId == R.id.sort) {
            mSort = v;
            mMaterial = null;
            mMaterailMode = null;
            mPresenter.getMaterialList(v.getValue());
//            mMaterialView.setEnabled(true);
//            mMaterialModeView.setEnabled(false);
        } else if (mClickId == R.id.material) {
            mMaterial = v;
            mMaterailMode = null;
            mPresenter.getMaterialModeList(v.getValue());
//            mMaterialModeView.setEnabled(true);
        } else if (mClickId == R.id.materialMode) {
            mMaterailMode =v ;
        } else if (mClickId == R.id.materialStatus) {
            mMaterailStatus = v;
        }
        showSelectInfo();
    }

    private void showSelectInfo() {
        System.out.println("showSelectInfo > " + mSort);
        if (mSort != null) {
            System.out.println("mSortName.setText");
            mSortName.setText(mSort.getName());
        }
        if (mMaterial != null) {
            mMaterialName.setText(mMaterial.getName());
        }
        if (mMaterailMode != null) {
            mMaterialModeName.setText(mMaterailMode.getName());
        }
        if (mMaterailStatus != null) {
            mMaterialStatusName.setText(mMaterailStatus.getName());
        }
    }


    @Override
    protected boolean needSelect(BarcodeInfo info) {
        return info.isNew();
    }

    @Override
    public void onResume() {
        super.onResume();
        showSelectInfo();
    }

    @OnClick({R.id.sort,R.id.material,R.id.materialMode,R.id.materialStatus})
    public void click(View v) {
        int id = v.getId();
        System.out.println("click id = " + id);
        mClickId = id;
        FragmentDataManager.INSTANCE.select(null);
        FragmentDataManager.INSTANCE.setData(null);
        if (id == R.id.sort) {
            System.out.println(mSorts != null);
            if (mSorts != null) {
                FragmentDataManager.INSTANCE.setData(mSorts);
            } else {
                mPresenter.getMaterialCategoryList();
            }
        } else if (id == R.id.material) {
            if (mSort == null) {
                showTip("请先选择类型");
                AnimatorHelper.shake(getContext(),mSortView);
                return;
            }
            mPresenter.getMaterialList(mSort.getValue());
        } else if (id == R.id.materialMode) {
            if (mMaterial == null) {
                showTip("请先选择名称");
                AnimatorHelper.shake(getContext(),mMaterialView);
                return;
            }
            mPresenter.getMaterialModeList(mMaterial.getValue());
        } else if (id == R.id.materialStatus) {
            if (mMaterialStatusList == null) {
                mPresenter.getWGStatusConfigs();
            } else {
                FragmentDataManager.INSTANCE.setData(mMaterialStatusList);
            }
        }

        NavHostFragment.findNavController(this).navigate(R.id.action_addAssetMainFragment_to_selectFragment);
    }

    @OnClick(R.id.add)
    public void add(View v) {
        Context context = getContext();
        if (context == null) {
            return;
        }
        if (mMaterial == null) {
            AnimatorHelper.shake(context,mMaterialView);
            return;
        }
        if (mMaterailMode == null) {
            AnimatorHelper.shake(context,mMaterialModeView);
            return;
        }
//        if (mMaterailStatus == null) {
//            AnimatorHelper.shake(context,mMaterialStatusName);
//            return;
//        }
        String barcode = mBarcodeEdit.getText().toString();
        if (TextUtils.isEmpty(barcode) && mSelects.size() == 0) {
            AnimatorHelper.shake(context,mBarcodeEdit);
            return;
        }
        List<String> barcodes = new ArrayList<>();
        if (!TextUtils.isEmpty(barcode)) {
            barcodes.add(barcode);
        } else {
            for (BarcodeInfo info : mSelects) {
                barcodes.add(info.getBarcode());
            }
        }
        String ms = null;
        if (mMaterailStatus != null) {
            ms = mMaterailStatus.getValue();
        }
        mPresenter.createStock(barcodes,mMaterial.getValue(),mMaterailMode.getValue(),ms);
    }

    @Override
    public void onCreateStoclResult(List<String> barcodes,boolean success,String msg) {
        Context context = getContext();
        if (context == null) {
            return;
        }
        if (success) {
//            reset();
            for (String b : barcodes) {
                for (BarcodeInfo info : mSelects) {
                    if (b.equals(info.getBarcode())) {
                        mData.remove(info);
                        mSelects.remove(info);
                        break;
                    }
                }
            }
            freshTip();
            mAdapter.notifyDataSetChanged();
            mBarcodeEdit.setText("");
            DialogUtil.showSuccessDialog(context,mRoot,"新增物资成功");
        } else {
            showTip(msg);
        }

    }

    private void reset() {
        mBarcodeEdit.setText("");
        mSort = null;
        mMaterial = null;
        mMaterailStatus = null;
        mMaterailMode = null;
        mData.clear();
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            mBarcodeEdit.setEnabled(false);
            mBarcodeEdit.setText("正在扫描中");
            mBarcodeEdit.setTextColor(Color.parseColor("#409EFE"));
        } else {
            mBarcodeEdit.setText("");
            mBarcodeEdit.setEnabled(true);
            mBarcodeEdit.setTextColor(Color.parseColor("#666666"));
        }
        super.onCheckedChanged(buttonView,isChecked);
    }

}
